package www.woniuxy.com.class112.controller;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
//import www.woniuxy.com.class112.dto.NoticeDto;
//import www.woniuxy.com.class112.entity.Notice;
import www.woniuxy.com.class112.dto.NoticeDto;
import www.woniuxy.com.class112.entity.Notice;
import www.woniuxy.com.class112.form.AddNoticeForm;
import www.woniuxy.com.class112.form.PageForm;
import www.woniuxy.com.class112.param.AddNoticeParam;
import www.woniuxy.com.class112.param.PageParam;
import www.woniuxy.com.class112.service.NoticeService;
import www.woniuxy.com.class112.util.JwtUtil;
import www.woniuxy.com.class112.util.Result;

import javax.annotation.Resource;

@RestController
@RequestMapping("/notice")
@Api(tags = "公告相关接口")
public class NoticeController {
    @Resource
    private NoticeService noticeService;

//    @RequestMapping("/loadAllNotice")
//    public Result loadAllNotice(Integer pageNum, Integer pageSize)throws Exception{
//        PageInfo<Notice> noticeDtoPageInfo = noticeService.loadAllNotice(pageNum,pageSize);
//        return Result.success("查询所有公告成功", noticeDtoPageInfo);
//    }
    @PostMapping("/loadAllNotice")
    @ApiOperation("分页查询所有公告")
    public Result loadAllNotice(@RequestBody PageForm form)throws Exception{
        PageInfo<NoticeDto> noticeDtoPageInfo = noticeService.loadAllNotice(BeanUtil.toBean(form, PageParam.class));
        return Result.success("查询所有公告成功", noticeDtoPageInfo);
    }
//    @RequestMapping("/addNotice")
//    public Result addNotice(@RequestBody AddNoticeForm form,@RequestHeader String token)throws Exception{
//        noticeService.addNotice(BeanUtil.toBean(form, AddNoticeParam.class), JwtUtil.getUserId(token));
//        return Result.success("添加公告成功",null);
//    }
    @PostMapping("/addNotice")
    @ApiOperation("添加公告")
    public Result addNotice(@RequestBody AddNoticeForm form,@RequestHeader String token)throws Exception {
        Long agentId = JwtUtil.getUserId(token);
        AddNoticeParam param = BeanUtil.toBean(form, AddNoticeParam.class);
        param.setCreaterId(agentId);
        noticeService.addNotice(param);
        return Result.success("添加公告成功", null);
    }
    @GetMapping("/deleteNoticeById")
    @ApiOperation("删除公告")
    public Result deleteNoticeById(Long id)throws Exception{
        noticeService.deleteNoticeById(id);
        return Result.success("删除公告成功",null);
    }
//    @RequestMapping("/deleteNoticeById/{id}")
//    public Result deleteNoticeById(@PathVariable Long id)throws Exception{
//        noticeService.deleteNoticeById(id);
//        return Result.success("删除公告成功",null);
//    }
    @GetMapping("/delistNotice")
    @ApiOperation("下架公告")
    public Result delistNotice(Long id)throws Exception{
        noticeService.delistNotice(id);
        return Result.success("下架公告成功",null);
    }
    @GetMapping("/enableNotice")
    @ApiOperation("发布公告")
    public Result enableNotice(Long id)throws Exception{
        noticeService.enableNotice(id);
        return Result.success("发布公告成功",null);
    }
//    @GetMapping("/noticedetail")
//    @ApiOperation("公告详情")
//    public Result noticedetail(Long noticeId)throws Exception{
//        return Result.success("公告详情",noticeService.noticedetail(noticeId));
//    }
//    @RequestMapping("/updateNotice")
//    public Result updateNotice(@RequestBody UpdateNoticeForm uf)throws Exception{
//        noticeService.updateNotice(uf.getId(),uf.getTitle(),uf.getContent());
//        return Result.success("修改公告成功",null);
//    }

}
